<?php

namespace App\Http\Controllers\Api\Record;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Api\DrugStoreRequest;
use App\Http\Requests\Api\HpiSubUpdateRequest;
use App\Models\MedicalHpiSub;
use App\Project\Filters\HpiSubFilter;
use Illuminate\Http\Request;

class DrugController extends ApiController
{

    public function subsequent(DrugStoreRequest $request, MedicalHpiSub $hpiSub)
    {
        $input = (new HpiSubFilter($request))->json();

        $result = $hpiSub->create($input);

        return $this->response(['id' => $result->id]);
    }

    public function subShow(Request $request, MedicalHpiSub $hpiSub)
    {
        $this->validate($request, ['record_id' => 'required'], ['record_id.required' => '缺少病历ID。']);

        $result = $hpiSub->where('record_id', $request->input('record_id'))
                         ->select(['id', 'medication'])->get()->toArray();

        return $this->response($result);
    }

    public function subUpdate(HpiSubUpdateRequest $request, MedicalHpiSub $hpiSub)
    {
        $input = (new HpiSubFilter($request))->json();

        if (isset($input['record_id'])) {
            unset($input['record_id']);
        }

        $result = $hpiSub->where('id', $input['id'])->update($input);

        return $this->responseForUpdate($result);
    }
}
